ControlGetFocus


Retrieves which control of the target window has input focus, if any.

ControlGetFocus, OutputVar [, WinTitle, WinText, ExcludeTitle, ExcludeText]

Parameters

OutputVar

The name of the variable in which to store the identifier of the control, which consists of its classname followed by its sequence number within its parent window, e.g. Button12.

WinTitle The title or partial title of the target window (the matching behavior is determined by SetTitleMatchMode). If this and the next 3 parameters are omitted, the Last Found Window will be used. If this is the letter A and the next 3 parameters are omitted, the active window will be used. To use a window class, specify ahk_class ExactClassName (shown by Window Spy). To use a process identifier (PID), specify ahk_pid %VarContainingPID%. To use a window group, specify ahk_group GroupName. To use a window's unique ID number, specify ahk_id %VarContainingID%. The search can be narrowed by specifying multiple criteria. For example: My File.txt ahk_class Notepad
WinText If present, this parameter must be a substring from a single text element of the target window (as revealed by the included Window Spy utility). Hidden text elements are detected if DetectHiddenText is ON.
ExcludeTitle Windows whose titles include this value will not be considered.
ExcludeText Windows whose text include this value will not be considered.

ErrorLevel

ErrorLevel is set to 0 if the control with input focus was successfully retrieved. Otherwise (e.g. the target window doesn't exist or none of its controls have input focus) it will be set to 1.

Remarks

The control retrieved by this command is the one that has keyboard focus, that is, the one that would receive keystrokes if the user were to type any.

The target window must be active to have a focused control. If the window is not active, OutputVar will be made blank.

If ControlFocus is executed repeatedly at a high frequency (i.e. every 500 ms or faster), it will probably disrupt the user's ability to double-click. One workaround is to call the OS's GetGUIThreadInfo() via DllCall. For example:

; This script retrieves the ahk_id (HWND) of the active window's focused control.
; This script requires Windows 98+ or NT 4.0 SP3+.
GuiThreadInfoSize = 48
VarSetCapacity(GuiThreadInfo, GuiThreadInfoSize)
InsertInteger(GuiThreadInfoSize, GuiThreadInfo, 0)
if not DllCall("GetGUIThreadInfo", uint, 0, str, GuiThreadInfo)
{
    MsgBox GetGUIThreadInfo() indicated a failure.
    return
}
FocusedHWND := ExtractInteger(GuiThreadInfo, 12)  ; Retrieve the hwndFocus field from the struct.
MsgBox % "The focused control's ahk_id (HWND) is " . FocusedHWND
; This ID can be used all control commands.  For example:
; ControlGetText, OutputVar,, ahk_id %FocusedHWND%

Window titles and text are case sensitive. Hidden windows are not detected unless DetectHiddenWindows has been turned on.

Related

ControlFocus, ControlMove, ControlClick, ControlGetText, ControlSetText, ControlSend

Example

ControlGetFocus, OutputVar, Untitled - Notepad
if ErrorLevel
    MsgBox, The target window doesn't exist or none of its controls has input focus.
else
    MsgBox, Control with focus = %OutputVar%